package com.earl.gsm.web.equipment;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.gsm.equipment.RentTable;
import com.earl.gsm.domain.gsm.equipment.RepositoryInfo;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.RentTableService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/equipment/rentTable")
public class RentTableController extends BaseController {

	private final Logger log = Logger.getLogger(RentTableController.class);

	@Autowired
	private RentTableService service;

	@RequestMapping(value = "/rentAllMsg", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "租借表所有信息", notes = "all message of rentTable", httpMethod = "POST", response = RentTable.class, responseContainer = "List")
	public ModelAndView getAllMsg() {
		log.debug("get all message of rentTable");
		return new ModelAndView("equipment/rentAllMsg", "rentList", service.findAll());
	}
	
	/**
     * 服务端预处理添加器材
     * @param
     * @return
     */
    @RequestMapping(value="/preAdd")
    @ApiOperation(value = "添加器材", notes = "add new equipment", httpMethod = "GET", response = User.class, responseContainer = "List")
    public String preAdd(){
        return "equipment/addRentTable";
    }  

	@RequestMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "租借器材", notes = "rent new equipment", httpMethod = "POST", response = DwzMessage.class)
	public ResponseEntity<DwzMessage> applyForMatch(RentTable rentTable) {
		service.save(rentTable);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setStatusCode(200);
		dwzMessage.setMessage("租借成功");
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/rentTable/rentAllMsg");
		dwzMessage.setNavTabId("租借详情");
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "删除选中租借表", notes = "delete selected rentTable", httpMethod = "GET", response = User.class, responseContainer = "List")
	public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id) {
		service.delete(id);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/rentTable/rentAllMsg");
		dwzMessage.setStatusCode(200);
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}
	
	/**
     * 用户端完善器材信息
     * @return
     */
    @RequestMapping(value="/preUpdate/{id}")
    @ApiOperation(value = "预处理更新赛事信息", notes = "find All User", httpMethod = "GET", response = RentTable.class, responseContainer = "List")
    public ModelAndView preUpdate(@PathVariable  Long id){
        return new ModelAndView("equipment/preRentTable","rent", service.findOne(id));
    }

	/**
	 * 服务端或者用户端更新赛事
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "更新租借表信息", notes = "update equipment", httpMethod = "GET", response = DwzMessage.class, responseContainer = "List")
	public ResponseEntity<DwzMessage> update(RentTable rentTable) {
		service.updateByPrimaryKeySelective(rentTable);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setMessage("更新成功");
		dwzMessage.setStatusCode(200);
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/rentTable/rentAllMsg");
		dwzMessage.setNavTabId("租借详情");
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}

}
